package com.gitee.srurl.core.event.bloom;

import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.core.constant.RedisConstant;
import com.gitee.srurl.core.event.EventBusHandle;
import com.gitee.srurl.core.event.SubscribeSupporter;
import com.gitee.srurl.core.util.redis.RedisBloomUtil;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author zxw
 * @date 2023/5/2 15:14
 */
@Slf4j
public class ShortIdBloomSubscribeSupporter implements SubscribeSupporter<BloomEventParams> {

    private volatile List<String> bloomKeys;

    @Override
    public void register() {
        EventBusHandle.register(this, type());
    }

    @Override
    public void unRegister() {
        EventBusHandle.unregister(this, type());
    }

    @Override
    public String type() {
        return "short-id-bloom";
    }

    @Override
    @Subscribe
    public void onSubscribe(BloomEventParams event) {

        String shortKey = event.getShortId();
        if (StrUtil.isBlank(shortKey)) {
            log.error("接受[{}]事件shortKey为空", type());
            return;
        }
        // 添加参数到布隆过滤器
        RedisBloomUtil.add(RedisConstant.Bloom.SHORT_ID_BLOOM_BASE_KEY, shortKey);
    }
}
